package com.example.ssmDemo.core.interceptors;

import com.example.ssmDemo.core.dto.MyResult;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;

/**
 * 模块名称：
 * 模块类型：
 * 编码人：刘彬
 * 生成时间：2023/2/23
 * 联系电话：
 * */
public class MyTestInterceptors implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        //获取已经登录的用户
        String username= (String) session.getAttribute("userName");
        ServletContext servletContext = request.getSession().getServletContext();
        String sessionId=session.getId();
        //已经登录的列表
        HashMap<String,String> loginMap= (HashMap<String, String>) servletContext.getAttribute("loginMap");
        String loginedsession= loginMap.get(username);
        System.out.println(username);
        String basePath=request.getScheme() +"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
//        if (loginMap == null) {
//            response.sendRedirect(basePath);
//            return false;
//        }
        if (username == null || ("").equals(username)||loginedsession!=sessionId){
            String userName= (String) session.getAttribute("userName");
            loginMap.remove(userName);
            servletContext.removeAttribute("loginMap");
            servletContext.setAttribute("loginMap",loginMap);
            session.invalidate();
            response.setCharacterEncoding("UTF-8");
            RequestDispatcher rd = request.getRequestDispatcher(basePath);
            MyResult myResult=new MyResult();
            myResult.setCode(9999);
            myResult.setMsg("请重新登录");
            request.setAttribute("loginMsg",myResult);
            rd.forward(request, response);
//            response.sendRedirect(basePath);
            return false;
        }
        System.out.println(request.getRequestURL());
        if (request.getRequestURL()==null){
            response.sendRedirect(basePath);
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
